package com.aaa.util;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MyUtil {
    public static Map jsonMap(String url) {
        url = "&" + url + "&";
        String reqKey = "&(.*?)=";
        Pattern patternKey = Pattern.compile(reqKey);
        Matcher matcherKey = patternKey.matcher(url);
        String reqValue = "((?<==).*?(?=&))";
        Pattern patternValue = Pattern.compile(reqValue);
        Matcher matcherValue = patternValue.matcher(url);
        Map<Object, Object> map = new HashMap<>();
        while (matcherKey.find()) {
            if (matcherValue.find()) {
                map.put(matcherKey.group(1), matcherValue.group(1));
            }
        }


        return map;
    }


    public static Map parameterNamesUtil(HttpServletRequest req, HttpServletResponse resp) {
        String mothed = req.getParameter("mothed");
        Enumeration<String> parameterNames = req.getParameterNames();
        Map<Object, Object> map = new HashMap<>();
        while (parameterNames.hasMoreElements()) {
            String key = parameterNames.nextElement();
            Object value = req.getParameter(key);
            map.put(key, value);
        }
        return map;
    }

    public static String newSql(String oldSql, Map<String, Object> map, boolean flag) {
        System.out.println(map);
        StringBuffer stringBuffer = new StringBuffer(oldSql);
        if (map != null && map.size() > 0) {

            Map<String, Object> searchValue = new HashMap<>();
            for (String key : map.keySet()) {
                if (key.contains("search")) {
                    searchValue.put(key.replace("search", ""), map.get(key));
                }
            }
            System.out.println(searchValue);
            for (Map.Entry<String, Object> entry : searchValue.entrySet()) {
                if (entry.getValue() != null && !entry.getValue().equals("")) {
                    stringBuffer.append(" and " + (String) entry.getKey() + " like '%" + entry.getValue() + "%'");
                }
            }
            if (flag) {
                Boolean field = Boolean.valueOf(ObjectUtil.isNotEmpty(map.get("field")));
                if (field.booleanValue()) {
                    stringBuffer.append(" order by " + map.get("field") + " " + map.get("order"));
                }
                Boolean page = Boolean.valueOf(ObjectUtil.isNotEmpty(map.get("page")));
                if (page.booleanValue()) {
                    Integer pages = Integer.valueOf(Integer.parseInt((new StringBuilder()).append(map.get("page")).append("").toString()));
                    Integer limit = Integer.valueOf(Integer.parseInt((new StringBuilder()).append(map.get("limit")).append("").toString()));
                    stringBuffer.append(" limit " + ((pages.intValue() - 1) * limit.intValue()) + "," + limit);
                }
            }
        }
        return stringBuffer.toString();
    }


    public static Map<String, String> makePasswordSalt(String password) {
        Map<String, String> map = new HashMap<>();

        byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();

        String salt = Base64.encode(key);

        SymmetricCrypto aes = null;


        aes = new SymmetricCrypto(SymmetricAlgorithm.AES, Base64.decode(salt));


        String encryptHex = aes.encryptHex(password);

        map.put("password", encryptHex);

        map.put("salt", salt);
        return map;
    }


    public static String makePasswordSalt(String password, String salt) {
        SymmetricCrypto aes = null;

        aes = new SymmetricCrypto(SymmetricAlgorithm.AES, Base64.decode(salt));


        String encryptHex = aes.encryptHex(password);
        return encryptHex;
    }


    public static Long objToLong(Object obj) {
        return Long.valueOf(obj + "");
    }
}

